package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.ShiroUser;
import cn.stylefeng.guns.modular.system.entity.Classification;
import cn.stylefeng.guns.modular.system.entity.Product;
import cn.stylefeng.guns.modular.system.mapper.ProductOrderMapper;
import cn.stylefeng.guns.modular.system.model.params.ProductParam;
import cn.stylefeng.guns.modular.system.service.ClassificationService;
import cn.stylefeng.guns.modular.system.service.ProductService;
import cn.stylefeng.guns.modular.system.service.impl.ProductServiceImpl;
import cn.stylefeng.guns.sys.core.constant.cache.Cache;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


/**
 * 产品表控制器
 *
 * @author 小飞
 * @Date 2019-06-26 09:56:27
 */
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {

    private String PREFIX = "/Product";

    @Autowired
    private ProductService productService;
    @Autowired
    private ClassificationService classificationService;
    @Autowired
    private ProductOrderMapper productOrderMapper;
    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/product.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/product_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/product_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ProductParam productParam) {
        Product product = new ProductServiceImpl().getEntity(productParam);
        product.setClassification(0L);
        //判断是否有重名
        Classification classificationJuege = classificationService.getOne(new QueryWrapper<Classification>()
        .like("name",product.getName()));
        if (ToolUtil.isNotEmpty(classificationJuege)){
            return ResponseData.error(202,"已存在该产品");
        }else { //自动生成分类子集
            Long categoryId = product.getClassification();
            Classification classification = new Classification();
            classification.setName(product.getName());
            classification.setPid((long) categoryId);
            classificationService.save(classification);

            //通过产品名字去查询当前分类的id
            Classification classificationLast = classificationService.getOne(new QueryWrapper<Classification>()
                    .like("name",product.getName()));

            if (ToolUtil.isNotEmpty(classification)){
                ShiroUser shiroUser = ShiroKit.getUserNotNull();
                product.setCreateUser(shiroUser.getName());
                product.setCreateTime(new Date());
                product.setClassification(classificationLast.getId().longValue());
                productService.save(product);
                return ResponseData.success();
            }else {
                return  ResponseData.error(201,"创建子节点失败");
            }
        }

    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ProductParam productParam) {
        this.productService.update(productParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ProductParam productParam) {
        //先删除 分类表的数据
        Product product = productService.getById(productParam.getId());
        classificationService.getBaseMapper().deleteById(product.getClassification());

        //删除产品表中的数据
        this.productService.delete(productParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ProductParam productParam) {
        Product detail = this.productService.getById(productParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-06-26
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ProductParam productParam) {

        return this.productService.findPageBySpec(productParam);
    }


    /**
     * 获取树结构
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTree")
    public List<ZTreeNode> getTree(){

        List<ZTreeNode> treeNodes = this.classificationService.tree();
        treeNodes.add(ZTreeNode.createParent());

        return  treeNodes;
    }

    
}


